import Service from '@/api/httpAxios'

type loginType = {
  [x: string]: any
  token: string
}
type resconfig = {
  resconfig?: Object
  id?: string
  offset?: number
  limit?: number
}
export interface bindResConf {
  ori_project_id?: string
  domain_name?: string
}
type bucket = {
  project_id: string
  region: string
  vpc_id?: string
}
export interface instances {
  name: string
  project_id: string
  resource_specification_id?: string
  region: string
  resource_specification: any
  offset?: number
  limit?: number
  id?: string
}
export interface absolutea {
  name: string | {
    [key: string]: string;
  }
  project_id?: string
  region: string
  new_name?: string | {
    [key: string]: string;
  }
  resource_specification: any
  resource_specification_id?: string
  flavor: Array<any>
  id?: string | {
    [key: string]: string;
  },
  instance_type?: string
}
export interface deleteList {
  project_id: string
  resource_specification_id: string
  id?: string
}
class UserService {
  public login = (username: string, password: string) => {
    return Service.post<loginType>('v1/user/login', {
      username,
      password
    })
  }

  public logout = () => {
    return Service.post<loginType>('v1/user/logout')
  }
  public getRegion = () => {
    return Service.get<any>('v1/user/support-regions')
  }
  public getOrigininfo = (username: string, region: string) => {
    return Service.get<loginType>(`v1/user/origininfo?username=${username}&region=${region}`)
  }
  public addUser = (obj: resconfig) => {
    const url = `/v1/admin/userinfo`
    return Service.post<any>(url, obj.resconfig)
  }
  public changePass = (obj: resconfig) => {
    const url = `/v1/user/changepass`
    return Service.put<any>(url, obj.resconfig)
  }
  public changeUser = (obj: resconfig) => {
    const url = `/v1/admin/userinfo`
    return Service.put<any>(url, obj.resconfig)
  }
  public changeSelf = (obj: resconfig) => {
    const url = `/v1/user/userinfo`
    return Service.put<any>(url, obj.resconfig)
  }
  public delUser = (id: string) => {
    const url = `/v1/admin/userinfo?id=${id}`
    return Service.delete<any>(url)
  }
  public getUserInfo = (id: string) => {
    return Service.get<loginType>(`v1/user/userinfo?id=${id}`)
  }
  public getUserResconfig = (obj: resconfig) => {
    return Service.get<loginType>('v1/user/resconfig', {
      params: {
        offset: obj.offset ? obj.offset - 1 : obj.offset,
        limit: obj.limit,
        id: obj.id
      }
    })
  }
  public getAdminResconfig = (obj: resconfig) => {
    return Service.get<loginType>('v1/admin/resconfig', {
      params: {
        offset: obj.offset ? obj.offset - 1 : obj.offset,
        limit: obj.limit
      }
    })
  }
  public getAlluserinfo = (obj: resconfig) => {
    return Service.get<any>('/v1/admin/alluser', {
      params: {
        offset: obj.offset ? obj.offset - 1 : obj.offset,
        limit: obj.limit
      }
    })
  }
  public createResconfig = (obj: resconfig) => {
    const url = `/v1/admin/resconfig`
    return Service.post<any>(url, obj.resconfig)
  }
  public changeResconfig = (obj: resconfig) => {
    const url = `/v1/admin/resconfig`
    return Service.put<any>(url, obj.resconfig)
  }
  public buildResconfig = (obj: bindResConf) => {
    const url = `/v1/admin/bindres`
    return Service.post<any>(url, obj)
  }
  public deleteResconfig = (id: string) => {
    const url = `/v1/admin/resconfig?id=${id}`
    return Service.delete<any>(url)
  }
  public resetPassWord = (id: string) => {
    const url = `/v1/admin/resetpw?id=${id}`
    return Service.post<any>(url)
  }
  public escalationPrivileges = (id: string) => {
    const url = `/v1/admin/upgrade?id=${id}`
    return Service.put<any>(url)
  }
  public registe = (username: string, password: string, phone?: string, email?: string) => {
    return Service.post<loginType>('user/registe', {
      username,
      password,
      phone,
      email
    })
  }

  public getBucket = (obj: bucket) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/user/bucket`
    return Service.get<any>(url, {
      params: {
        region: obj.region ? obj.region : localStorage.getItem('region')
      }
    })
  }

  getImageList = (obj: bucket) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/user/image`
    return Service.get<any>(url, { params: { region: obj.region ? obj.region : localStorage.getItem('region') } })
  }
  getMirrorImage = (obj: bucket) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/user/swr-image`
    return Service.get<any>(url, { params: { region: obj.region ? obj.region : localStorage.getItem('region') } })
  }
  getDockerImage = (obj: bucket) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/builds/support-docker-image`
    return Service.get<any>(url, { params: { region: obj.region ? obj.region : localStorage.getItem('region') } })
  }
  getOrganization = (obj: bucket) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/user/swr-organization`
    return Service.get<any>(url, { params: { region: obj.region ? obj.region : localStorage.getItem('region') } })
  }
  getSystem = (obj: bucket) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/builds/support-image`
    return Service.get<any>(url, { params: { region: obj.region ? obj.region : localStorage.getItem('region') } })
  }
  getVpcList = (obj: bucket) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/user/vpc`
    return Service.get<any>(url, { params: { region: obj.region ? obj.region : localStorage.getItem('region') } })
  }
  getIdList = (obj: bucket) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/bandwidths`
    return Service.get<any>(url, { params: { region: obj.region ? obj.region : localStorage.getItem('region') } })
  }
  getSubnetList = (obj: bucket) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/user/subnet`
    return Service.get<any>(url, { params: { region: obj.region ? obj.region : localStorage.getItem('region'), vpc_id: obj.vpc_id } })
  }
  getAvailableZone = (obj: bucket) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/available-zone`
    return Service.get<any>(url, { params: { region: obj.region ? obj.region : localStorage.getItem('region') } })
  }
  getSecurityGroup = (obj: bucket) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/user/inbound-permission`
    return Service.get<any>(url, { params: { region: obj.region ? obj.region : localStorage.getItem('region') } })
  }
  getResourceSpecification = (obj: bucket) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/user/resource-specification`
    return Service.get<any>(url, { params: { region: obj.region ? obj.region : localStorage.getItem('region') } })
  }
  getInstanceList = (obj: instances) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/resource-specification`
    return Service.get<any>(url, {
      params: {
        offset: obj.offset ? obj.offset - 1 : obj.offset,
        limit: obj.limit,
        resource_specification_id: obj.resource_specification_id,
        name: obj.name
      }
    })
  }
  createInstanceList = (obj: absolutea) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/resource-specification`
    return Service.post<any>(url, obj)
  }
  updataInstanceList = (obj: absolutea) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/resource-specification`
    return Service.put<any>(url, obj)
  }
  deleteInstanceList = (obj: deleteList) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/resource-specification/${obj.resource_specification_id}`
    return Service.delete<any>(url)
  }
  detailsInstanceData = (obj: deleteList) => {
    let pid
    obj.project_id ? (pid = obj.project_id) : (pid = localStorage.getItem('project_id'))
    const url = `/v1/${pid}/resource-specification/${obj.resource_specification_id}`
    return Service.get<any>(url)
  }
}
export default new UserService()
